package edu.hebeu.activity;

import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import edu.hebeu.R;
import edu.hebeu.entity.CodeAndMsgResponse;
import edu.hebeu.entity.SearchUserResponse;
import edu.hebeu.network.API;
import edu.hebeu.network.APIConfig;
import edu.hebeu.network.MyCallback;
import edu.hebeu.util.DateUtil;

public class UserPerfectActivity extends BaseActivity {

    private static final String TAG = "UserPerfectActivity";

    private EditText et_username;
    private EditText et_password;
    private EditText et_repassword;
    private EditText et_name;
    private RadioButton rbtn_man;
    private RadioButton rbtn_weman;
    private EditText et_tel;
    private EditText et_address;
    private Button btn_submit;

    private Integer sex = 1; // 1-男，2-女

    private Date birthday; // 保存请求来 出生日期的信息
    private Long money; // 保持请求来 资金的信息
    private Integer business; // 保存请求来 是否为商家的信息

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_perfect;
    }

    @Override
    protected void initViews() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        et_repassword = (EditText) findViewById(R.id.et_repassword);
        et_name = (EditText) findViewById(R.id.et_name);
        rbtn_man = (RadioButton) findViewById(R.id.rbtn_man);
        rbtn_weman = (RadioButton) findViewById(R.id.rbtn_weman);
        et_tel = (EditText) findViewById(R.id.et_tel);
        et_address = (EditText) findViewById(R.id.et_address);
        btn_submit = (Button) findViewById(R.id.btn_submit);

        et_name.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)}); // 设置该组件的输入最大长度为10位
        et_password.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); // 设置该组件的输入最大长度为20位
        et_repassword.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); // 设置该组件的输入最大长度为20位
        et_tel.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)}); // 设置该组件的输入最大长度为11位
        et_address.setFilters(new InputFilter[]{new InputFilter.LengthFilter(40)}); // 设置该组件的输入最大长度为40位

        getUserInfo();
        initListeners();
    }

    private void initListeners() {
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = et_password.getText().toString();
                String re_password = et_repassword.getText().toString();
                if ("".equals(password)) {
                    showToast("请输入您的密码", 0, false);
                    return;
                }
                if (!password.equals(re_password)) {
                    showToast("密码不一致", 0, false);
                    return;
                }
                Map<String, Object> params = new HashMap<>();
                params.put("username", et_username.getText().toString());
                params.put("password", et_password.getText().toString());
                params.put("name", et_name.getText().toString());
                params.put("sex", sex);
                if (birthday == null) {
                    params.put("birthday", DateUtil.formDate("yyyy-MM-dd HH:mm:ss", new Date()));
                } else {
                    params.put("birthday", DateUtil.formDate("yyyy-MM-dd HH:mm:ss", birthday));
                }
                params.put("tel", et_tel.getText().toString());
                params.put("address", et_address.getText().toString());
                params.put("money", money);
                params.put("business", business);
                requestUserPerfect(params);
            }
        });
    }

    /**
     * 该方法用来将请求的信息加载到UI组件上
     */
    private void show() {

    }

    /**
     * 男的单选框被点击
     * @param view
     */
    public void rbtnManClick(View view) {
//        showToast("男点击", 0, false);
        sex = 1;
    }

    /**
     * 女的单选框被点击
     * @param view
     */
    public void rbtnWeManClick(View view) {
//        showToast("女点击", 0, false);
        sex = 2;
    }

    /**
     * 获取用户的信息，并赋值到UI界面
     */
    private void getUserInfo() {
        Map<String, Object> params = new HashMap<>();
        params.put("username", getSPFile("username", ""));
        requestUserInfo(params);
    }

    /**
     * 该方法用来请求用户信息
     * @param params
     */
    private void requestUserInfo(Map<String, Object> params) {
        String token = (String) getSPFile("token", "");
        API.requestConfig(APIConfig.URL.SEARCH_USER_URL, params, token)
            .getRequest(new MyCallback() {
                @Override
                public void onSuccess(String res) {
                    SearchUserResponse response = (SearchUserResponse) deSerialize(res, SearchUserResponse.class);
                    if (response.getCode() == 200) {
                        final SearchUserResponse.User searchUserInfo = response.getUser();
                        Log.i(TAG, "请求用户信息成功" + searchUserInfo);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showToast(searchUserInfo.toString(), 1, false); // 不进行同步处理的吐司弹窗
                                et_username.setText(searchUserInfo.getUsername());
                                et_password.setText(searchUserInfo.getPassword());
                                et_name.setText(searchUserInfo.getName());
                                if (searchUserInfo.getSex() == 1) {
                                    rbtn_man.setSelected(true);
                                } else if (searchUserInfo.getSex() == 2) {
                                    rbtn_weman.setSelected(true);
                                }
                                et_tel.setText(searchUserInfo.getTel());
                                et_address.setText(searchUserInfo.getAddress());

                                birthday = searchUserInfo.getBirthday();
                                money = searchUserInfo.getMoney();
                                business = searchUserInfo.getBusiness();
                            }
                        });
                    } else if (response.getCode() == 401) {
                        jumpActivity(LoginActivity.class, true);
                        showToast("身份验证失败，请重新登录！", 1, true); // 注意，进行同步处理的吐司弹窗最好放到最后
                    }

                }

                @Override
                public void onFailure(Exception e) {
                    Log.e(TAG, "请求用户信息失败！");
                    showToast("请求用户信息失败！", 0, true);
                }
            }, true);
    }

    /**
     * 该方法用来请求完善用户信息
     * @param params
     */
    private void requestUserPerfect(Map<String, Object> params) {
        API.requestConfig(APIConfig.URL.PERFECT_INFO_USER, params, "")
            .postRequest(new MyCallback() {
                @Override
                public void onSuccess(String res) {
                    CodeAndMsgResponse response = (CodeAndMsgResponse) deSerialize(res, CodeAndMsgResponse.class);
                    if (response.getCode() == 200) {
                        Log.i(TAG, "完善用户信息成功");
                        showToast("信息完善成功！", 0, true);
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    Log.e(TAG, "请求完善用户信息失败！");
                    showToast("完善用户信息失败！", 0, true);
                }
            }, false);
    }
}
